package com.yc.jpyy.mobile.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.base.redis.JedisTemplate;
import com.yc.jpyy.base.redis.RedisManager;

@Controller
@RequestMapping(value = "/terminalRedis")
public class TerminalRedis extends BaseController{
	@RequestMapping(value = "/AddSignRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String AddSignRecord(HttpServletRequest request) {
		RestResponse response = new RestResponse();
		String teminalno = request.getParameter("teminalno");
		String userno = request.getParameter("userno");
		String usertype = request.getParameter("usertype");
		if(teminalno != null && !teminalno.isEmpty() && userno != null 
				&& !userno.isEmpty()&& usertype != null && !usertype.isEmpty()){
			JedisTemplate jedisTemplate = (JedisTemplate)RedisManager.getCacheManagerImpl(null);
			String key = teminalno+"_"+userno+"_"+usertype;
			try {
				Set<String> result = jedisTemplate.getKeys("*"+userno+"*");
				if(result != null && result.size() >0){
					List<String> keys = new ArrayList<>(result);
					jedisTemplate.deleteKey(keys);
				}
				jedisTemplate.setKeyValue(key, new Date().getTime()+"_"+key);
				response.setErrorcode(0);
			} catch (Exception e) {
				e.printStackTrace();
				response.setErrorcode(1);
				response.setMessage("保存失败...");
			}
		}
		return response.toJSON();
	}
	
	@RequestMapping(value = "/DelSignRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String DelSignRecord(HttpServletRequest request) {
		RestResponse response = new RestResponse();
		String userno = request.getParameter("userno");
		if(userno != null && !userno.isEmpty()){
			JedisTemplate jedisTemplate = (JedisTemplate)RedisManager.getCacheManagerImpl(null);
			try {
				Set<String> result = jedisTemplate.getKeys("*"+userno+"*");
				if(result != null && result.size() >0){
					List<String> keys = new ArrayList<>(result);
					jedisTemplate.deleteKey(keys);
				}
				response.setErrorcode(0);
			} catch (Exception e) {
				e.printStackTrace();
				response.setErrorcode(1);
				response.setMessage("删除失败...");
			}
		}
		return response.toJSON();
	}
	
	@RequestMapping(value = "/selectSignRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectSignRecord(HttpServletRequest request) {
		RestResponse response = new RestResponse();
		String pattern = "*" + request.getParameter("pattern") + "*";
		if(pattern != null && !pattern.isEmpty()){
			JedisTemplate jedisTemplate = (JedisTemplate)RedisManager.getCacheManagerImpl(null);
			try {
				Set<String> result = jedisTemplate.getKeys(pattern);
				response.setErrorcode(0);
				StringBuilder data = new StringBuilder();  
			    boolean flag = false;  
			    for (String string : result) {  
			        if (flag) {  
			            data.append(",");  
			        } else {  
			            flag = true;  
			        }  
			        data.append(string);  
			    }  
			    response.setMessage(data.toString());
			} catch (Exception e) {
				e.printStackTrace();
				response.setErrorcode(1);
				response.setMessage("查詢失败...");
			}
		}
		return response.toJSON();
	}
}
